package com.pianke.yc.test.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.pianke.yc.test.Activity.DetailsActivity;
import com.pianke.yc.test.Adapter.JobAdapter;
import com.pianke.yc.test.Classess.HttpMethod;
import com.pianke.yc.test.Classess.Job;
import com.pianke.yc.test.Classess.JobLab;
import com.pianke.yc.test.Net.NetObject;
import com.pianke.yc.test.R;
import com.pianke.yc.test.View.LoadMoreListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YC on 2016/3/31.
 */
public class AcceptFragment extends Fragment implements NetObject.FailCallback, NetObject.SuccessCallback, ListView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, LoadMoreListView.OnLoadMore {

    private static final String URL = "http://192.168.56.1:8080/jianzhiTest/Job";
    private JobLab mJobLab;
    private SwipeRefreshLayout swip;
    private LoadMoreListView mListView;
    private static List<Job> mJobs;
    private JobAdapter mJobAdapter;
    private int load = 0;
    private int type = 1;
    private int start = 0;
    private int limit = 10;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_accept, container, false);
        mListView = (LoadMoreListView) v.findViewById(R.id.accept_list);
        mListView.setLoadMoreListen(this);
        mListView.setOnItemClickListener(this);

        swip = (SwipeRefreshLayout) v.findViewById(R.id.accept_swip);
        swip.setOnRefreshListener(this);
        swip.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);

        init();
        return v;
    }

    private void init() {
        mJobLab = JobLab.get(getContext());
        loadMore();
        if (mJobs == null) {
            mJobs = new ArrayList<>();
        }
        mJobAdapter = new JobAdapter(getContext(), mJobs);
        mListView.setAdapter(mJobAdapter);
    }

    private void initData(int page2, List<Job> list) {
        switch (page2) {
            case 1:
                for (Job j : list)
                    mJobs.add(j);
                break;
            case -1:
                for (int i = 0; i < list.size(); i++) {
                    Job p = list.get(i);
                    if (p.id == mJobs.get(i).id) {
                        break;
                    } else {
                        mJobs.add(i, p);
                    }
                }
                Toast.makeText(getContext(), "加载完成", Toast.LENGTH_SHORT).show();
                break;
        }
        type = 0;
        mJobAdapter.notifyDataSetChanged();
    }

    private Map<String, String> getParmes(int type) {
        Map<String, String> parmes = new HashMap<>();
        if (type == 1) {
            parmes.put("start", start + "");
            parmes.put("numble", limit + "");
        } else {
            parmes.put("start", "0");
            parmes.put("numble", "10");
        }
        return parmes;
    }


    private List<Job> getList(String s) {
        List<Job> lists = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                Job j = new Job((JSONObject) jsonArray.get(i));
                lists.add(j);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return lists;
    }

    @Override
    public void onRefresh() {
        type = -1;
        new NetObject(URL, getParmes(type), HttpMethod.POST, this, this).execute();
        swip.setRefreshing(false);
    }

    @Override
    public void loadMore() {
        type = 1;
        if (load == 1) {
            List<Job> list = mJobLab.getJobs(mJobs.get(mJobs.size() - 1).id, 5);
            initData(type,list);
        } else {
            new NetObject(URL, getParmes(type), HttpMethod.POST, this, this).execute();
        }
        start += limit;
        mListView.onLoadComplete();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getContext(), DetailsActivity.class);
        intent.putExtra("id", mJobs.get(position).id);
        startActivity(intent);

    }

    @Override
    public void onFail() {
        Toast.makeText(getContext(), "加载失败", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(String a) {
        List<Job> list = getList(a);
        if ( list.size() > 0 && list.get(list.size() - 1).id <= mJobLab.getId()) {
            load = 1;
        }
        initData(type, list);
    }

    @Override
    public void onDestroy() {
        super.onPause();
        mJobLab.mJobs.clear();
        for (Job j:mJobs)
            mJobLab.addJob(j);

        mJobLab.get(getContext()).savaJobs();
    }

}
